package com.yuwei.movie.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.yuwei.database.entity.MovieEntityItem
import com.yuwei.image.glide.GlideImageLoader
import com.yuwei.movie.R
import com.yuwei.movie.databinding.MovieListItemBinding

import kotlin.math.max


class MovieListAdapter constructor(private val list: MutableList<MovieEntityItem>) : RecyclerView.Adapter<MovieListAdapter.ItemViewHolder>(){

    private var itemClickListener: OnItemClickListener? = null

    private var scrollSate = RecyclerView.SCROLL_STATE_IDLE //滚动停止

    var preloadMoreData: (()->Unit)? = null

    var preloadEndCount = 0

    private var isReloading = false

    inner class ItemViewHolder(val dataBinding: MovieListItemBinding): RecyclerView.ViewHolder(dataBinding.root)

    interface OnItemClickListener{
        fun onItemClick(v: View, position: Int)
    }
    fun getData(position: Int): MovieEntityItem {
            return list[position]
    }
    fun getClos(){
        list.clear()
        notifyDataSetChanged()
    }

    fun addItems(items: List<MovieEntityItem>) {
        list.addAll(items)
        notifyDataSetChanged() // 通知插入范围
    }
    fun setItemOnClickListener(l: OnItemClickListener){
        itemClickListener = l
    }

    override fun getItemCount(): Int = list.size

    override fun getItemViewType(position: Int): Int {
        return 0
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(MovieListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        //标题和描述
        holder.dataBinding.movieListName.text = entity.title
        holder.dataBinding.movieListText.text = entity.description
        GlideImageLoader.loadImage(holder.dataBinding.root.context,holder.dataBinding.movieListIv,entity.videomainimag)



        //点击事件
        holder.dataBinding.movieListIv.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }

        holder.dataBinding.movieListEnshrine.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }





        //检查是否自动加载下一页数据
        checkPreloadMoreData(position)
    }


    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                scrollSate = newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }

    private fun checkPreloadMoreData(position: Int) {
        if (scrollSate != RecyclerView.SCROLL_STATE_IDLE //滚动中
            && position == max(list.size - 1 - preloadEndCount, 0)
            && !isReloading){
            isReloading = true
            preloadMoreData?.invoke() //执行预加载方法
        }else{
            isReloading = false
        }

    }


}